package config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;
import java.util.EnumSet;

public class SystemConfig extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{MvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement("c:/tmp", 10240000, 102400000, 1024000));
    }


    /**
     * 注册字符编码过滤器，此过滤器就是针对DispatchServlet而言的
     * 也就是说请求能进到DispatchServlet，此过滤器就生效，不进mvc，此字符编码过滤器就不生效
     *
     * 此过滤器可以解决接收前端传递过来的数据的乱码问题
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter charEncodingFilter = new CharacterEncodingFilter();
        charEncodingFilter.setEncoding("UTF-8");
        charEncodingFilter.setForceEncoding(true);

        return new Filter[]{charEncodingFilter};
    }


}
